//
//  MovieListViewController.m
//  UI-豆瓣项目APP
//
//  Created by dllo on 15/9/8.
//  Copyright (c) 2015年 孙鹏. All rights reserved.
//

#import "MovieListViewController.h"
#import "MovieListTableViewCell.h"
#import "AppTools.h"
#import "Movielist.h"
#import "MBProgressHUD.h"
#import "MovieDetalViewController.h"
#import "MovieList9View.h"
#import "MovieList9ViewController.h"
#import "DateBaseSingleton.h"
@interface MovieListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,retain)MBProgressHUD *hud;

@end

@implementation MovieListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.navigationItem.title = @"电影";
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    self.movieTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStylePlain];
    
    self.movieTableView.delegate = self;
    self.movieTableView.dataSource = self;
    [self.view addSubview:self.movieTableView];
    [_movieTableView release];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(changeMovieList)];
    
    self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.hud show:YES];
    self.hud.labelText = @"正在加载中.....";
    
    
    [self.movieTableView registerClass:[MovieListTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
  
    self.allKeys = [NSMutableArray array];
    
    
   self.allKeys = [[DateBaseSingleton shareDateBase]selectMovie];
    
    

    
    [self getAFNetWorkingDate];
    
    
     [self.hud hide:YES];
    
   
    
    // Do any additional setup after loading the view.
}

- (void)changeMovieList
{
    
    MovieList9ViewController *movieList9VC = [[MovieList9ViewController alloc]init];
    
    [self.navigationController pushViewController:movieList9VC animated:YES];
   
    
    
}





- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    
    return self.allKeys.count;
    
    
}




-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return 135;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifer = @"cell";
    
    MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifer];
   
    Movielist *movie = [self.allKeys objectAtIndex:indexPath.section];
    
    cell.amovie = movie;
        cell.selectionStyle = UITableViewCellSeparatorStyleNone;

    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;
    
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MovieDetalViewController *movieDVC = [[MovieDetalViewController alloc]init];
    
    movieDVC.amovie = [self.allKeys objectAtIndex:indexPath.section];
    movieDVC.indexpath = indexPath.row;

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [self.navigationController pushViewController:movieDVC animated:YES];
    
    
}

-(void)getAFNetWorkingDate
{
    
    
    [AppTools getDataWithFileURL:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" block:^(NSMutableDictionary *result) {
        
        [[DateBaseSingleton shareDateBase]dropMovieTable];
        [[DateBaseSingleton shareDateBase]creatMovieTable];
        
        NSMutableArray *array = [result objectForKey:@"result"];
        
        for (NSMutableDictionary *dic in array) {
            
            Movielist *movie = [[Movielist alloc]init];
            
            [movie setValuesForKeysWithDictionary:dic];
            
//            movie.userName = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
            
            [[DateBaseSingleton shareDateBase]insertMovie:movie];
            
            [self.allKeys addObject:movie];
            
            
            
        }
        
        [self.movieTableView reloadData];
        [self.hud hide:YES];
        
    }];

    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
